/* Microsoft Reference Implementation for TPM 2.0
 *
 *  The copyright in this software is being made available under the BSD License,
 *  included below. This software may be subject to other third party and
 *  contributor rights, including patent rights, and no such rights are granted
 *  under this license.
 *
 *  Copyright (c) Microsoft Corporation
 *
 *  All rights reserved.
 *
 *  BSD License
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this list
 *  of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright notice, this
 *  list of conditions and the following disclaimer in the documentation and/or
 *  other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "Tpm.h"
#include "NV_UndefineSpaceSpecial_fp.h"
#include "SessionProcess_fp.h"

#if CC_NV_UndefineSpaceSpecial  // Conditional expansion of this file

/*
This command allows removal of a platform-created NV Index that has TPMA_NV_POLICY_DELETE
SET.
This command requires that the policy of the NV Index be satisfied before the NV Index may be deleted.
Because administrative role is required, the policy must contain a command that sets the policy command
code to TPM_CC_NV_UndefineSpaceSpecial. This indicates that the policy that is being used is a policy
that is for this command, and not a policy that would approve another use. That is, authority to use an
entity does not grant authority to undefine the entity.
Since the index is deleted, the Empty Buffer is used as the authValue when generating the response
HMAC.
If nvIndex is not defined, the TPM shall return TPM_RC_HANDLE.
If nvIndex references an Index that has its TPMA_NV_PLATFORMCREATE or
TPMA_NV_POLICY_DELETE attribute CLEAR, the TPM shall return TPM_RC_ATTRIBUTES.
NOTE An Index with TPMA_NV_PLATFORMCREATE CLEAR may be deleted with
TPM2_UndefineSpace()as long as shEnable is SET. If shEnable is CLEAR, indexes created using
Owner Authorization are not accessible even for deletion by the platform

该命令允许删除具有TPMA_NV_POLICY_DELETE属性的平台创建的NV索引。在删除NV索引之前，该命令需要满足NV索引的策略。
由于需要管理角色，因此策略必须包含一个命令，将策略命令代码设置为TPM_CC_NV_UndefineSpaceSpecial。
这表示正在使用的策略是适用于此命令的策略，而不是批准其他用途的策略。也就是说，使用实体的权限并不授予取消定义该实体的权限。
由于删除了索引，因此在生成响应HMAC时使用空缓冲区作为authValue。如果未定义nvIndex，则TPM将返回TPM_RC_HANDLE。
如果nvIndex引用的索引的TPMA_NV_PLATFORMCREATE或TPMA_NV_POLICY_DELETE属性被清除，则TPM将返回TPM_RC_ATTRIBUTES。
注意：具有TPMA_NV_PLATFORMCREATE清除的索引可以使用TPM2_UndefineSpace()删除，只要shEnable被设置。
如果shEnable被清除，则使用Owner授权创建的索引即使对于平台删除也是不可访问的。
*/
/*(See part 3 specification)
// Delete a NV index that requires policy to delete.
*/
//  Return Type: TPM_RC
//      TPM_RC_ATTRIBUTES               TPMA_NV_POLICY_DELETE is not SET in the
//                                      Index referenced by 'nvIndex'
TPM_RC
TPM2_NV_UndefineSpaceSpecial(
    NV_UndefineSpaceSpecial_In  *in             // IN: input parameter list
    )
{
    TPM_RC           result;
    NV_REF           locator;
    NV_INDEX        *nvIndex = NvGetIndexInfo(in->nvIndex, &locator);
// Input Validation
    // This operation only applies when the TPMA_NV_POLICY_DELETE attribute is SET
    if(!IS_ATTRIBUTE(nvIndex->publicArea.attributes, TPMA_NV, POLICY_DELETE))   
        return TPM_RCS_ATTRIBUTES + RC_NV_UndefineSpaceSpecial_nvIndex;
// Internal Data Update
    // Call implementation dependent internal routine to delete NV index
    result = NvDeleteIndex(nvIndex, locator);

    // If we just removed the index providing the authorization, make sure that the
    // authorization session computation is modified so that it doesn't try to
    // access the authValue of the just deleted index
    if(result == TPM_RC_SUCCESS)
        SessionRemoveAssociationToHandle(in->nvIndex);
    return result;
}

#endif // CC_NV_UndefineSpaceSpecial